/**
 * Created by Administrator on 2018/11/13 0013.
 */

$(function () {
    require([
        "text!/pcCom/index-table.ejs"
    ],function (indexTabel){
        var currPage = 0;
        var pageSize = 20;
        var $total = $("#total");
        var total = +$total.text();
        var nextBtn = $("#nextBtn");
        var prevBtn = $("#prevBtn");
        var $tbody = $("table tbody");
        var $cover = $(".cover");
        var $level = $cover.find("#level");
        var $type = $cover.find("#type");
        var $password = $cover.find("#password");

        setBtns();
        $("table").on("click","span.del",function () {
           var me = $(this);
           var sid = me.data("sid");
            window.wxc.xcConfirm("确认删除吗?", window.wxc.xcConfirm.typeEnum.confirm,{
                onOk:function () {
                    console.log("点击了确定");
                    $.ajax({
                        url:"/admin/api/del",
                        type:"post",
                        data:{
                            id:sid
                        },
                        success:function (ret) {
                            console.log(ret);
                            window.location.reload();
                        }
                    })
                }
            });
        });
        $("table").on("click","span.edit",function () {
            var me = $(this);
            var sid = me.data("sid");
            $cover.removeClass("hide");
            $cover.sid = sid;
        });

        $cover.on("click","span#cancel",function () {
            $cover.addClass("hide");
            $cover.sid = null;
        })

        $cover.on("click","span#confirm",function () {
            var sid =   $cover.sid;
            $cover.addClass("hide");
            $cover.sid = null;
            var level = $level.val().trim() || '';
            var typ = $type.val().trim() || '';
            var password = $password.val().trim() || '';
            if(password && password.length < 6){
                window.wxc.xcConfirm("密码长度不能小于6", window.wxc.xcConfirm.typeEnum.warning)
                return ;
            }
            if(sid){
                $.ajax({
                    url:"/admin/api/update",
                    type:"post",
                    data:{
                        level:level,
                        type:typ,
                        password:password,
                        id:sid
                    },
                    success:function (ret) {
                        console.log(ret);
                        window.location.reload();
                    }
                })
            }
        });
        

        //设置上下页的btn
        function setBtns(){

            if(currPage > 0){
                prevBtn.removeClass("disabled");
            }else{
                prevBtn.addClass("disabled");
            }

            if((currPage + 1)*pageSize < total){
                nextBtn.removeClass("disabled");
            }else{
                nextBtn.addClass("disabled");
            }

            $("#pages").html(currPage+1);
            $total.html(total);
        }

        //上页
        prevBtn.click(function(){
            var me = $(this);
            if(!me.hasClass("disabled") && currPage > 0){
                currPage--;
                getFriends();
            }
        });

        //下页
        nextBtn.click(function(){
            var me = $(this);
            if(!me.hasClass("disabled")){
                currPage++;
                getFriends();
            }
        });

        //点击搜索
        $("#search").on("change",function(){
            var val = this.value.trim();
            currPage = 0;
            if(val){
                searchData(val);
            }else{
                getFriends();
            }
        });
        //搜索
        function searchData(val){
            $.ajax({
                url:"/admin/api/search",
                type:"post",
                dataType: 'json',
                data:{
                    val:val
                },
                success:function (ret) {
                    total = ret.data.list.length;
                    setBtns();
                    renderList(ret.data.list);
                }
            })
        }

        //获取好友
        function getFriends(){
            $.ajax({
                url:"/admin/api/getuser",
                type:"post",
                dataType: 'json',
                data:{
                    currPage:currPage,
                    pageSize:pageSize
                },
                success:function (ret) {
                    console.log(ret);
                    total = ret.data.total || 0;
                    setBtns();
                    renderList(ret.data.list);
                }

            })
        }

        function renderList(list) {
            list = list || [];
            var html = ejs.render(indexTabel,{list:list});
            $tbody.html(html);
        }


        


    })
});